package com.itheima.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisConstant;
import com.itheima.domain.Setmeal;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.service.SetmealService;
import com.itheima.utils.QiniuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.io.IOException;
import java.util.List;
import java.util.UUID;

/**
 * 套餐功能控制层
 */
@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    @Autowired
    private JedisPool jedisPool;

    @Reference
    private SetmealService setmealService;

    /**
     * 图片上传
     *
     * @return
     */
    @RequestMapping("/upload")
    public Result upload(@RequestParam("imgFile") MultipartFile imgFile) {
        String originalFilename = imgFile.getOriginalFilename();
        String s = originalFilename.substring(originalFilename.lastIndexOf(".") - 1);
        String fileName = UUID.randomUUID().toString() + s;
        try {
            QiniuUtils.upload2Qiniu(imgFile.getBytes(), fileName);
            Jedis jedis = jedisPool.getResource();
            jedis.sadd(RedisConstant.SETMEAL_PIC_RESOURCES, fileName);
            jedis.close();
        } catch (IOException e) {
            e.printStackTrace();
            return Result.failed(MessageConstant.PIC_UPLOAD_FAIL);
        }
        return Result.success(MessageConstant.PIC_UPLOAD_SUCCESS, fileName);
    }

    /**
     * 套餐的新增
     *
     * @return
     */
    @PostMapping("/add")
    public Result add(@RequestBody Setmeal setmeal, Integer[] checkgroupIds) {
        try {
            setmealService.add(setmeal, checkgroupIds);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.failed(MessageConstant.ADD_SETMEAL_FAIL);
        }
        return Result.success(MessageConstant.ADD_SETMEAL_SUCCESS);
    }

    /**
     * 套餐分页查询
     *
     * @return
     */
    @PostMapping("/findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean) {
        try {
            PageResult result = setmealService.findByCondition(queryPageBean);
            return Result.success(MessageConstant.QUERY_SETMEAL_SUCCESS, result);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.failed(MessageConstant.QUERY_SETMEAL_FAIL);
        }
    }

    /**
     * 删除套餐
     *
     * @param id
     * @return
     */
    @PostMapping("/deleteSetmeal")
    public Result deleteSetmeal(Integer id) {
        try {
            setmealService.deleteSetmeal(id);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.failed(MessageConstant.DELETE_SETMEAL_FAIL);
        }
        return Result.success(MessageConstant.DELETE_SETMEAL_SUCCESS);
    }

    /**
     * 根据套餐ID查询关联的检查组ID
     *
     * @param id
     * @return
     */
    @PostMapping("/findCheckGroupIds")
    public Result findCheckGroupIds(Integer id) {
        try {
            Integer[] ids = setmealService.findAssocateCheckGroupIds(id);
            return Result.success(MessageConstant.QUERY_CHECKGROUP_SUCCESS, ids);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.failed(MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
    }

    /**
     * 根据ID查询套餐
     *
     * @param id
     * @return
     */
    @PostMapping("/findById")
    public Result findById(Integer id) {
        try {
            Setmeal setmeal = setmealService.findById(id);
            return Result.success(MessageConstant.QUERY_SETMEAL_SUCCESS, setmeal);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.failed(MessageConstant.QUERY_SETMEAL_FAIL);
        }
    }

    /**
     * 修改套餐
     *
     * @return
     */
    @PostMapping("/edit")
    public Result edit(@RequestBody Setmeal setmeal, Integer[] checkgroupIds) {
        try {
            setmealService.edit(setmeal, checkgroupIds);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.failed(MessageConstant.EDIT_SETMEAL_FAIL);
        }
        return Result.success(MessageConstant.EDIT_SETMEAL_SUCCESS);
    }
}
